package org.wisdom.ac.application.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.wisdom.ac.domain.resource.entity.AcAppPage;
import org.wisdom.ac.domain.resource.repository.ResourceRepository;
import org.wisdom.ac.domain.role.entity.AcRole;
import org.wisdom.ac.domain.role.entity.AcRoleResource;
import org.wisdom.ac.domain.role.repository.RoleRepository;
import org.wisdom.ac.domain.user.entity.AcUser;
import org.wisdom.ac.domain.user.entity.AcUserRole;
import org.wisdom.ac.domain.user.repository.UserRepository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 启动类
 *
 * @author 张智研
 * @date 2022/06/14 20:55
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class AcServiceRunner implements ApplicationRunner {
    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;
    private final ResourceRepository resourceRepository;
    private final RoleRepository roleRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        String[] menuIds = {"ac-app-page","ac-user","ac-role"};
        String[] menuNames = {"菜单管理","用户管理","角色管理"};
        AcAppPage appPage;
        boolean b;
        for(int i = 0; i < menuIds.length; i++) {
            b =  resourceRepository.existsById(menuIds[i]);
            if(!b) {
                appPage = new AcAppPage();
                appPage.setAppId("wisdom-ac");
                appPage.setId(menuIds[i]);
                appPage.setCode(menuIds[i]);
                appPage.setPermission(menuIds[i]);
                appPage.setTitle(menuNames[i]);
                appPage.setAppPageWidgets(new ArrayList<>());
                appPage.setAppPageFacades(new ArrayList<>());
                appPage.setOutside(false);
                appPage.setHidden(false);
                appPage.setStatus(true);
                resourceRepository.save(appPage);
            }
        }
        String name = "admin";
        b =  roleRepository.existsById(name);
        if (!b) {
            AcRole role = new AcRole();
            role.setId(name);
            role.setName("超级管理员");
            role.setEnabled(true);
            List<AcRoleResource> roleResourceList = new ArrayList<>();
            AcRoleResource roleResource;
            for (String menuId : menuIds) {
                roleResource = new AcRoleResource();
                roleResource.setRoleId(name);
                roleResource.setResourceId(menuId);
                roleResourceList.add(roleResource);
            }
            role.setRoleResources(roleResourceList);
            roleRepository.save(role);
        }
        b =  userRepository.existsById(name);
        if (!b) {
            AcUser user = new AcUser();
            user.setId(name);
            user.setUserName(name);
            user.setPassword(passwordEncoder.encode("123456"));
            user.setEnabled(true);
            AcUserRole userRole = new AcUserRole();
            userRole.setUserId(name);
            userRole.setRoleId(name);
            List<AcUserRole> userRoleList = new ArrayList<>();
            userRoleList.add(userRole);
            user.setUserRoles(userRoleList);
            userRepository.save(user);
        }
    }
}
